function confirmDelete() {
	var deleteYN=false;
	var answer = confirm("Do you want to delete this client?")
	if (answer !=0) {
		deleteYN=true;
	}
	return deleteYN;
}

function enableFieldForClientupdate(theForm) {
	document.getElementById('clientName').disabled=false;
	document.getElementById('clientBusinessKey').disabled=false;
	document.getElementById('clientDomain').disabled=false;
	document.getElementById('startDateCalender').disabled=false;
	document.getElementById('endDateCalender').disabled=false;
	document.getElementById('clientActive').disabled=false;
	document.getElementById('discount').disabled=false;
	document.getElementById('currencyId').disabled=false;
	document.getElementById('saveUpdateButton').style.display="block";
	document.getElementById('updateButton').style.display="none";
}
function saveUpdatedClientDetails(theForm) {
	document.forms.updateClient.action="maintenance.html?method=saveUpdateClient";
	document.forms.updateClient.submit();
}
function validateonSearchSubmit(theForm) {
	var criteriaSet=false;
	
	var cName=document.getElementById("searchClientName");
	if (cName.value != null && cName.value.length != 0) {
		criteriaSet=true;
	}
	
	var sDate=document.getElementById("searchStartDate");
	if (sDate.value != null && sDate.value.length != 0) {
		criteriaSet=true;
	}
	
	var sCreateOn=document.getElementById("searchCreatedOn");
	if (sCreateOn.value != null && sCreateOn.value.length != 0) {
		criteriaSet=true;
	}
	alert(criteriaSet);
	if (criteriaSet==false) {
		alert('Please provide search criteria to continue');
		return false;
	} else {
		return true;
	}
}
function validateFormBeforeSubmit(theForm){
	var reason = "";
	var clientName = validateMe(document.getElementById('clientName'),'Client Name');
	reason += clientName;
	var startDateCalender = validateMe(document.getElementById('startDateCalender'),'Start Date');
	reason += startDateCalender;
	var discount = validateMe(document.getElementById('discount'),'Discount');
	reason += discount;
	
	
	if (reason != "") {
		 alert("Errors:\n" + reason); 
		return false;
	}
	
	if (document.getElementById('currencyId').value == "Select"){
		alert("Please select a currency");
		return false;
	}
	
	if (!isValidDiscount()){
	return false;
	}else{
	document.forms["updateClient"].action = "maintenance.html?method=saveUpdateClient";
	document.forms.updateClient.submit();
	}
	
}



function isValidDiscount(){
var discount = document.getElementById("discount").value;

       var ValidChars = "0123456789.";
         var IsValidChar=true;
         var Char;
         var count = 0;
         for (i = 0; i < discount.length && IsValidChar == true; i++)
         {
            Char = discount.charAt(i);
            if(Char == ' '){
             alert("Discount should not contain space.\n");
             return false;
            }
            
            if(Char == '.'){
            count =  count+1;
            }
            if(ValidChars.indexOf(Char) == -1 || count>1)
            {
            alert("Please enter valid Discount.")
            return false;
            }
         }
         
         if(discount>100){
           alert("Discount can not be more than 100% .")
            return false;
         }
         
         return true;
}

function cDate(nd) {
	// this is the symbol that separates months, days and years and you can set
	// it to anything, just pick your separator
	var separator = "/";

	// puts mm, dd and yyyy into an array
	var ndarr = nd.split(separator);

	// saves each argument of the array into a string starting with the day
	nd = ndarr[1] + separator + ndarr[0] + separator + ndarr[2];
	return nd;
}


function validateFormBeforeSubmitForAddClient(theForm){
	var reason = "";
	var clientName = validateMe(document.getElementById('clientName'),'Client Name');
	reason += clientName;
	var startDateCalender = validateMe(document.getElementById('startDateCalender'),'Start Date');
	reason += startDateCalender;
	var discount = validateMe(document.getElementById('discount'),'Discount');
	reason += discount;
	
	
	var strtDt = document.getElementById('startDateCalender');
	var endDt = document.getElementById('endDateCalender');
	var chStrt = cDate(strtDt.value);
	var chEDt = cDate(endDt.value);
	if(Date.parse(chStrt) > Date.parse(chEDt)) {
		reason += "End date cannot be before start date. \n"
	}
	
	if (reason != "") {
		 alert("Errors:\n" + reason); 
		return false;
	} 	
	
	if (document.getElementById('currencyId').value == "Select"){
		alert("Please select a currency");
		return false;
	}
	
	if(!isValidDiscount()){
	return false;
	}else {
		return true;
	}
	
}

function validateMe(fld,name) {
	var error = "";

	if (fld != null &&(fld.value == null || (fld.value != null && fld.value.length == 0)) ) {
		fld.style.background = 'Yellow'; 
		error = name+" field has not been filled in.\n"
	} else {
		var numaric = fld.value;
		for(var j=0;j < numaric.length ; j++)
		{
			if(j==0){
			var alphaa = numaric.charAt(j);
			var chCode = alphaa.charCodeAt(0);
			if(chCode == "32"){
				fld.style.background = 'Yellow'; 
				error = name+" should not start with space.\n"
			}
			}
		}
		fld.style.background = 'White';
	}
	return error;  
	}

function validateForAddClientAddress(theForm) {
	var opts = document.getElementById("chooseClient").options;
	for(i=0; i < opts.length;i++) {
		if(opts[i].selected) {
			if(opts[i].value == -1) {
				document.getElementById("addressLine1").value="";
				document.getElementById("addressLine2").value="";
				document.getElementById("city").value="";
				document.getElementById("county").value="";
				document.getElementById("country").value="";
				document.getElementById("postCode").value="";
				document.getElementById("phoneNumber").value="";
				document.getElementById("alternativePhoneNumber").value="";
				document.getElementById("faxNumber").value="";
				alert("Please select any client");
				return false;
			} else {
				document.getElementById('mainTableDiv').style.display="block";
			}
		}
	}
}

function validateForAddClientAddressFormSubmit(theForm) {
	var reason = "";
	var addressLine1 = validateMe(document.getElementById('addressLine1'),'Address Line 1');
	reason += addressLine1;
	var city = validateMe(document.getElementById('city'),'City');
	reason += city;
	var county = validateMe(document.getElementById('county'),'County');
	reason += county;
	var country = validateMe(document.getElementById('country'),'Country');
	reason += country;
	var postCode = validateMe(document.getElementById('postCode'),'Post Code');
	reason += postCode;
	
	if (reason != "") {
		 alert("Errors:\n" + reason); 
		return false;
	} 
	document.forms["addClientAddress"].action = "maintenance.html?method=saveClientAddress";
	document.forms.addClientAddress.submit();
}

